package cn.itsource.hbc.org.controller;


import cn.itsource.hbc.org.domain.Shop;

import cn.itsource.hbc.org.domain.ShopLog;
import cn.itsource.hbc.org.query.ShopQuery;
import cn.itsource.hbc.org.service.IShopService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){

        try {
            if(shop.getId()!=null){
                shopService.update(shop);
            }else {
                shopService.save(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }

    }



    @GetMapping
    public List<Shop> loadAll(){

        return shopService.loadAll();
    }


    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id") Long id){

        return shopService.loadById(id);

    }



    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }

    }

    @PostMapping
    public PageResult<Shop> queryPage(@RequestBody ShopQuery query){
        System.out.println(query);
        return  shopService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult allDelete(@RequestBody Long[]ids){
        try {
            shopService.allDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }


    }



    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        System.out.println(shop);
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }




    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopLog shopLog){
        try {
            shopService.reject(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopLog shopLog){
        try {
            shopService.pass(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }


}
